using System;
using System.Text;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;
using OracleInternal.Network;

namespace OracleInternal.TTC
{
	internal class TTCSessionRelease : TTCFunction
	{
		internal const short SESSRLS_DROPSESS = 1;

		internal const short SESSRLS_DEAUTHENTICATE = 2;

		internal const short SESSRLS_RETAG = 4;

		internal const short SESSRLS_MULTIPROPERTY_TAG = 8;

		private string m_sessrlstag;

		private long m_sessrlsmode;

		internal TTCSessionRelease(MarshallingEngine mEngine)
			: base(mEngine, 163, 0)
		{
			m_ttcCode = 26;
		}

		internal void ReleaseSession(string drcpTagName, bool bUseDRCPMultiTag)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCSessionRelease, OracleTraceFuncName.ReleaseSession);
			}
			try
			{
				WriteFunctionHeader();
				byte[] array = null;
				m_sessrlsmode = 0L;
				if (drcpTagName != null)
				{
					array = m_marshallingEngine.m_dbCharSetConv.ConvertStringToBytes(drcpTagName, 0, drcpTagName.Length);
					m_marshallingEngine.MarshalPointer();
					m_marshallingEngine.MarshalSWORD(array.Length);
					m_sessrlsmode |= 4L;
					if (m_marshallingEngine.NegotiatedTTCVersion >= 8 && bUseDRCPMultiTag)
					{
						m_sessrlsmode |= 8L;
					}
				}
				else
				{
					m_marshallingEngine.MarshalSWORD(0);
					m_marshallingEngine.MarshalNullPointer();
				}
				m_marshallingEngine.MarshalUB4(m_sessrlsmode);
				if (array != null)
				{
					m_marshallingEngine.MarshalCHR(array);
				}
				if (26 == m_ttcCode)
				{
					m_marshallingEngine.m_oraBufWriter.FlushData();
				}
				else
				{
					ReceiveResponse();
				}
				m_marshallingEngine.m_bDRCPSessionAttached = false;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCSessionRelease, OracleTraceFuncName.ReleaseSession);
				}
			}
		}

		internal void ReceiveResponse()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCSessionRelease, OracleTraceFuncName.ReceiveResponse);
			}
			bool flag = false;
			try
			{
				m_marshallingEngine.TTCErrorObject.Initialize();
				while (!flag)
				{
					try
					{
						switch (m_marshallingEngine.UnmarshalSB1())
						{
						case 23:
							ProcessServerSidePiggybackFunction(0);
							break;
						case 8:
							ReadRPAMessage();
							break;
						case 15:
							m_marshallingEngine.TTCErrorObject.ReadWarning();
							break;
						case 4:
							m_marshallingEngine.TTCErrorObject.ReadErrorMessage();
							flag = true;
							break;
						case 9:
							if (m_marshallingEngine.HasEOCSCapability)
							{
								m_marshallingEngine.m_endOfCallStatus = m_marshallingEngine.UnmarshalUB4();
							}
							if (m_marshallingEngine.HasFSAPCapability)
							{
								m_marshallingEngine.m_endToEndECIDSequenceNumber = m_marshallingEngine.UnmarshalUB2();
							}
							flag = true;
							break;
						default:
							throw new Exception("TTC Error");
						}
					}
					catch (NetworkException ex)
					{
						if (ex.ErrorCode != 3111)
						{
							throw;
						}
						m_marshallingEngine.m_oracleCommunication.Reset();
					}
					catch (Exception)
					{
						if (m_marshallingEngine.m_oraBufRdr != null)
						{
							m_marshallingEngine.m_oraBufRdr.ClearState();
						}
						m_marshallingEngine.m_oracleCommunication.Break();
						m_marshallingEngine.m_oracleCommunication.Reset();
					}
				}
			}
			catch (Exception ex3)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCSessionRelease, OracleTraceFuncName.ReceiveResponse, ex3);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCSessionRelease, OracleTraceFuncName.ReceiveResponse);
				}
			}
		}

		internal void ReadRPAMessage()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCSessionRelease, OracleTraceFuncName.ReadRPAMessage);
			}
			try
			{
				long num = m_marshallingEngine.UnmarshalUB4();
				if (num > 0)
				{
					byte[] bytes = m_marshallingEngine.UnmarshalCHR((int)num);
					m_sessrlstag = Encoding.ASCII.GetString(bytes);
				}
				m_sessrlsmode = m_marshallingEngine.UnmarshalUB4();
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCSessionRelease, OracleTraceFuncName.ReadRPAMessage);
				}
			}
		}
	}
}
